package com.liujit.standard.basis.generator.action.config;

import com.liujit.standard.basis.generator.config.DataSourceConfig;
import com.liujit.standard.basis.generator.engine.TemplateEngine;
import com.liujit.standard.basis.generator.service.GeneratorService;
import com.liujit.standard.basis.generator.action.model.TableInfo;
import com.liujit.standard.basis.generator.engine.base.AbstractTemplateEngine;
import com.liujit.standard.basis.generator.engine.config.ContextConfig;

import java.util.List;

/**
 * @Description 代码生成的抽象配置
 * @Author LiuJun
 * @Date 2020/8/13 2:59 下午
 */
public abstract class AbstractGeneratorConfig {

    ContextConfig contextConfig = new ContextConfig();

    DataSourceConfig dataSourceConfig = new DataSourceConfig();

    List<TableInfo> tableInfo;

    String columnStr;

    /**
     * 初始化配置
     */
    protected abstract void config();

    public void init() {
        config();
        GeneratorService generatorService = new GeneratorService();
        List<TableInfo> tableInfo = generatorService.setDataSourceConfig(dataSourceConfig)
                .getTableInfo(contextConfig.getTableName());
        this.tableInfo = tableInfo;
        String columnStr = "";
        for (TableInfo item : tableInfo) {
            columnStr += ", " + item.getName();
        }
        this.columnStr = columnStr.substring(2);
    }

    public void doGeneration() {
        init();
        AbstractTemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setContextConfig(contextConfig);
        templateEngine.setTableInfo(tableInfo);
        templateEngine.setColumnStr(columnStr);
        templateEngine.start();
    }
}
